package com.itliang.service.hardware.impl;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.itliang.core.model.dto.equipment.EquipmentDataQueryRequest;
import com.itliang.core.model.entity.equipment.DataTypeRange;
import com.itliang.core.model.entity.equipment.EquipmentData;
import com.itliang.core.model.entity.equipment.Statistics;
import com.itliang.enums.DataType;
import com.itliang.mapper.hardware.EquipmentDataMapper;
import com.itliang.service.hardware.DataTypeRangeService;
import com.itliang.service.hardware.EquipmentDataService;
import com.itliang.utils.easyexcel.EasyExcelUtils;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import java.util.Date;
import java.util.List;
import java.util.Map;
import java.util.Objects;

/**
 * @author liangzhuoyuan
 * @description 针对表【equipment_data(数据表)】的数据库操作Service实现
 */
@Service
public class EquipmentDataServiceImpl extends ServiceImpl<EquipmentDataMapper, EquipmentData>
        implements EquipmentDataService {

    @Resource
    private EquipmentDataMapper equipmentDataMapper;

    @Resource
    private DataTypeRangeService dataTypeRangeService;

    @Override
    public void insertMqttCollectionData(Map<String, EquipmentData> map) {
    }

    @Override
    public Page<EquipmentData> listEquipmentDataByPage(Page page, EquipmentDataQueryRequest equipmentDataQueryRequest) {
        return equipmentDataMapper.listEquipmentDataByPage(page, equipmentDataQueryRequest);
    }

    @Override
    public void exportEquipmentDataExcelByDataType(EquipmentDataQueryRequest queryRequest, HttpServletResponse response) {
        String equipmentCode = queryRequest.getEquipmentCode();
        Integer isException = queryRequest.getIsException();
        String beginTime = queryRequest.getBeginTime();
        String endTime = queryRequest.getEndTime();
        boolean b = isException != null && (isException == 0 || isException == 1 || isException == 2);
        QueryWrapper<EquipmentData> wrapper = new QueryWrapper<>();
        wrapper.eq("data_type", queryRequest.getDataType())
                .like(StringUtils.hasText(equipmentCode), "equipment_code", equipmentCode)
                .eq(b, "is_exception", isException)
                .ge(StringUtils.hasText(beginTime), "collection_time", beginTime)
                .le(StringUtils.hasText(endTime), "collection_time", endTime);

        List<EquipmentData> list = this.list(wrapper);
        String message = DataType.getMessageByDataType(queryRequest.getDataType());
        EasyExcelUtils.writeExcel(list, EquipmentData.class, message + "数据", response);
    }

    /**
     * 把硬件数据存入数据库
     *
     * @param code
     * @param data
     * @param dataType
     * @param collectionTime
     */
    @Override
    public void insertHardData(String code, Double data, Integer dataType, Date collectionTime) {
        // 根据dataType查询预警范围值
        DataTypeRange range = dataTypeRangeService.getOne(new QueryWrapper<DataTypeRange>().eq("data_type", dataType));
        // 判断数据情况
        Integer determineData = determineData(range, data);
        // 存入数据库
        EquipmentData equipmentData = new EquipmentData();
        equipmentData.setIsException(determineData);
        equipmentData.setData(data);
        if (!Objects.isNull(range)) {
            equipmentData.setMinData(range.getMinData());
            equipmentData.setMaxData(range.getMaxData());
        }
        equipmentData.setEquipmentCode(code);
        equipmentData.setDataType(dataType);
        equipmentData.setCollectionTime(collectionTime);

        this.save(equipmentData);
    }

    @Override
    public Statistics getStatisticsData(Statistics statistics) {
        Statistics statisticsData = equipmentDataMapper.getStatisticsData(statistics);
        statisticsData.setDataType(statistics.getDataType());
        statisticsData.setCountBeginDate(statistics.getCountBeginDate());
        statisticsData.setCountEndDate(statistics.getCountEndDate());
        return statisticsData;
    }

    private Integer determineData(DataTypeRange range, Double data) {
        if (range == null) {
            return 0;
        }
        Double minData = range.getMinData();
        Double maxData = range.getMaxData();
        if (data >= minData && data <= maxData) {
            return 1;
        } else {
            // TODO 发送邮件预警
            return 2;
        }
    }

}




